package com.fzj.example.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * ClassName: FileDemo
 * date: 2019/11/7 15:38
 * Description: TODO
 * D:\self_domain\server\language\JAVA\java培训\java基础\source\project\basic_example\io\test.txt
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class FileByteDemo {
    public static void main(String[] args) throws IOException {
        byte[] bytes=new byte[]{65,66,77,78};
        File file=new File(new File("").getAbsolutePath()+File.separator+"io"+File.separator+"test.txt");
        if(!file.exists()){
            file.getParentFile().mkdir();
            file.createNewFile();
        }
        // 写入二进制文件，直接打开会出现乱码
        // append追加由参数决定
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        try {
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileOutputStream.close();
        }

        //输入流
        FileInputStream fileInputStream = new FileInputStream(new File("").getAbsolutePath() + "/io/test.txt");
        int c;
        // 读取写入的二进制文件，输出字节数组
        while ((c = fileInputStream.read()) != -1) {
            System.out.println(new String(String.valueOf((char)c)));
            System.out.println(c);
        }
        fileInputStream.close();
    }
}
